import getNetWork from '../../libs/getNetWork.js';
import portlet from '../../components/portlet.vue';
let id = 1000;
export default {
    components:{
        portlet,
    },
    data() {
        return {
            data2: [
             ],
            defaultProps: {
              children: 'children',
              label: 'menu_name'
            },
            name:'',
            operate_name:'',
            checked:null,
            loading:false,
            showCheck:true,
            dialogFormVisible0:false,
            dialogFormVisible1:false,
            dialogFormVisible2:false,
            dialogFormVisible3:false,
            dialogFormVisible_addOperate:false,
            menu_id:"",
            form0: {
              tree_name: '',
              tree_url: '',
              tree_sort:'',
            },
            form1: {
              id:'',
              tree_name: '',
              tree_url: '',
              tree_sort:'',
            },
            form2: {
              name: '',
            },
            form3: {
              id:'',
              operate_name: '',
              operate: '',
              menu_id:'',
            },
            form_addOperate: {
              operate_name: '',
              operate: '',
            },
            operateData:[],
        };
    },

    mounted:function() {
        this.$nextTick(()=>{
            this.getData();
        })
        
    },
    methods:{
        getData(){
                this.loading=true;
                let that=this;
                let data={
                    url:that.getNetDataAddress+'/menu/getMenuTree',
                    successCallback(res){
                        that.data2=res.body.dataArr;
                    },
                    defaultCallback(){
                        that.loading=false;
                    }
                }
                getNetWork.get(this,data);

        },
        getDataPower(menu_id){
               this.loading=true;
               let that=this;
               let data={
                url:that.getNetDataAddress+'/menu/getMenuOperate',
                data:{
                    menu_id:menu_id,
                    operate_name:that.operate_name,
                },
                successCallback(res){
                    that.operateData=res.body.dataArr;
                }
            }
            getNetWork.post(this,data);

        },
        changeData(){
            this.loading=true;
                let that=this;
                let data={
                    url:that.getNetDataAddress+'/role/updateRole',
                    data:{
                        page:that.page,
                        rows:that.rows,
                        rolename:that.name,
                    },
                    successCallback(res){
                        that.tableData=res.body.dataObj.rows;
                        that.total=res.body.dataObj.records;
                        that.loading=false;
                    },
                    defaultCallback(){
                        that.loading=false;
                    }
                }
                getNetWork.post(this,data);
                this.getData();
        },
        search(){
            this.getDataPower(this.menu_id);
        },
        closeMenu(){
            this.form0.menu_name='';
            this.form0.url='';
            this.form0.menu_code='';
        },
        add(){
            this.dialogFormVisible0=false;

            let that=this;
            console.log(that.checked.id);
            let data={
                url:that.getNetDataAddress+'/menu/addMenu',
                data:{
                    tree_name:that.form0.tree_name,
                    tree_url:that.form0.tree_url,
                    tree_sort:that.form0.tree_sort,
                    tree_parid:that.checked.id,
                },
                successCallback(res){
                    that.$message('添加成功！');
                    that.getData();
                }
            }
            getNetWork.post(this,data); 
        },
        update(){
            this.dialogFormVisible1=false;
            let that=this;
            let data={
                url:that.getNetDataAddress+'/menu/updateMenu',
                data:{
                    tree_name:that.form1.tree_name,
                    id:that.form1.id,
                    tree_url:that.form1.tree_url,
                    tree_sort:that.form1.tree_sort,
                },
                successCallback(res){
                    that.$message('修改成功！');
                    that.getData();
                }
            }
            getNetWork.post(this,data); 
        },

        updateOperate(){
            if(this.checked==null){
                this.$message({
                    type: 'info',
                    message: '请选择要修改的行！'
                  });
                return;
            }
            this.dialogFormVisible3 = true;
            this.form3.id=this.checked.id;
            this.form3.operate_name = this.checked.operate_name;
            this.form3.operate = this.checked.operate;
            this.form3.menu_id = this.checked.menu_id;
           
        },


        handleTableChange(val) {
            this.checked=val;
            console.log(this.checked);
        },
        append(store, data) {
            this.dialogFormVisible0=true;
            this.checked=data;
            console.warn('gggg')
            console.log(this.checked.id)
        },
        beforeUpdate(store, data1) {
            this.dialogFormVisible1=true;
            let that=this;
            let data={
                url:that.getNetDataAddress+'/menu/selectMenuById',
                data:{
                    id:data1.id,
                },
                successCallback(res){
                    that.form1.tree_name=res.body.dataObj.tree_name;
                    that.form1.tree_sort=res.body.dataObj.tree_sort;
                    that.form1.tree_url=res.body.dataObj.tree_url;
                    that.form1.id=res.body.dataObj.id;
                }
            }
            getNetWork.post(this,data); 
            
        },
        beforePower(store, data1) {
            this.menu_id = data1.id;
            let that=this;
            let data={
                url:that.getNetDataAddress+'/menu/getMenuOperate',
                data:{
                    menu_id:data1.id,
                },
                successCallback(res){
                    that.operateData=res.body.dataArr;
                }
            }
            getNetWork.post(this,data);
            this.dialogFormVisible2=true;
        },
        remove(store, data1) {
            this.$confirm('此操作将删除该项, 是否继续?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {

                let that=this;
                let data={
                    url:that.getNetDataAddress+'/menu/delMenu',
                    data:{
                        id:data1.id,
                    },
                    successCallback(res){
                        that.$message('删除成功！');
                        that.getData();
                    }
                }
                console.log('del');
                getNetWork.post(this,data);
            }).catch(() => {
                console.log('error');
            });
        },
        
        saveOperate(){
            this.dialogFormVisible3=false;
            let that=this;
            let data={
                url:that.getNetDataAddress+'/menu/updateOperate',
                data:{
                    id:that.form3.id,
                    operate:that.form3.operate,
                    operate_name:that.form3.operate_name,
                },
                successCallback(res){
                    that.$message('修改成功！');
                    that.getDataPower(that.form3.menu_id);
                }
            }
            getNetWork.post(this,data); 
        },
       
         saveAddOperate(){
            this.dialogFormVisible_addOperate=false;
            let that=this;
            let data={
                url:that.getNetDataAddress+'/menu/addOperate',
                data:{
                    operate:that.form_addOperate.operate,
                    operate_name:that.form_addOperate.operate_name,
                    menu_id:that.menu_id,
                },
                successCallback(res){
                    that.$message('添加成功！');
                    that.getDataPower(that.menu_id);
                }
            }
            that.form_addOperate.operate = '';
            that.form_addOperate.operate_name= '';
            getNetWork.post(this,data); 
        },
        deletOperate() {
            if(this.checked==null){
                this.$message({
                    type: 'info',
                    message: '请选择要修改的行！'
                  });
                return;
            }
            this.$confirm('此操作将删除该项, 是否继续?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {

                let that=this;
                let data={
                    url:that.getNetDataAddress+'/menu/delOperate',
                    data:{
                        id:that.checked.id,
                    },
                    successCallback(res){
                        that.$message('删除成功！');
                        that.getDataPower(that.menu_id);
                    }
                }
                console.log('del');
                getNetWork.post(this,data);
            }).catch(() => {
                console.log('error');
            });
        },
        renderContent(h, { node, data, store }) {
            if(data.level==0){
                return (
                  <span>
                    <span>
                      <span>{node.label}</span>
                    </span>
                    <span style="position:absolute;right:20px;top:0px">
                      <el-button size="mini" on-click={ () => this.append(store, data) }>新增</el-button>
                    </span>
                  </span>);
            }else if(data.level==1){
                if(data.children.length != 0){
                    return (
                      <span>
                        <span>
                          <span>{node.label}</span>
                        </span>
                        <span style="position:absolute;right:20px;top:0px">
                          <el-button size="mini" on-click={ () => this.append(store, data) }>新增</el-button>
                          <el-button size="mini" on-click={ () => this.beforeUpdate(store, data) }>修改</el-button>
                        </span>
                      </span>);
    
                }else{
                    return (
                      <span>
                        <span>
                          <span>{node.label}</span>
                        </span>
                        <span style="position:absolute;right:20px;top:0px">
                          <el-button size="mini" on-click={ () => this.append(store, data) }>新增</el-button>
                          <el-button size="mini" on-click={ () => this.remove(store, data) }>删除</el-button>
                          <el-button size="mini" on-click={ () => this.beforeUpdate(store, data) }>修改</el-button>
                        </span>
                      </span>);
                }
                
            }else if(data.level==2){
                return (
                  <span>
                    <span>
                      <span>{node.label}</span>
                    </span>
                    <span style="position:absolute;right:20px;top:0px">
                      <el-button size="mini" on-click={ () => this.remove(store, data) }>删除</el-button>
                      <el-button size="mini" on-click={ () => this.beforeUpdate(store, data) }>修改</el-button>
                      
                    </span>
                  </span>);
            }
            
        }
    }
};
